from mcp.server.fastmcp import FastMCP
import math

# Create an MCP server
mcp = FastMCP("Math Calculator Server")

@mcp.tool()
def add(a: float, b: float) -> float:
    """Addition of two numbers"""
    return a + b

@mcp.tool()
def subtract(a: float, b: float) -> float:
    """Subtraction of two numbers"""
    return a - b

@mcp.tool()
def multiply(a: float, b: float) -> float:
    """Multiplication of two numbers"""
    return a * b

@mcp.tool()
def divide(a: float, b: float) -> float:
    """Division of two numbers"""
    if b == 0:
        raise ValueError("Cannot divide by zero")
    return a / b

@mcp.tool()
def power(base: float, exponent: float) -> float:
    """Calculate base raised to the power of exponent"""
    return base ** exponent

@mcp.tool()
def square_root(x: float) -> float:
    """Calculate square root of a number"""
    if x < 0:
        raise ValueError("Cannot calculate square root of negative number")
    return math.sqrt(x)

@mcp.tool()
def logarithm(x: float, base: float = math.e) -> float:
    """Calculate logarithm of a number with optional base (default is natural log)"""
    if x <= 0 or base <= 0 or base == 1:
        raise ValueError("Invalid input for logarithm")
    return math.log(x, base)

if __name__ == "__main__":
    mcp.run(transport="stdio")